Has anyone managed to get a tool

您所在的位置:网站首页 call result Has anyone managed to get a tool

Has anyone managed to get a tool

2024-07-09 22:57| 来源: 网络整理| 查看: 265

Discovered what was happening in my case (OpenAI Python SDK 1.3). With the previous streaming implementation for function or content, you were always able to determine the type of response in the first chunk. With tool calls, the first chunk actually can have content, function, and tool_calls all set to None and so you have to sniff multiple chunks from the response before you can determine what kind of response you are accumulating.

There is additional caveat that a tool call chunk delta always presents an array of length 1 which contains an object that has the index inside of it. This was non-obvious from looking at the OpenAPI spec. Here’s an example of such a delta:

delta: ChoiceDelta( content=None, function_call=None, role=None, tool_calls=[ ChoiceDeltaToolCall( index=0, id='call_uGViZDuQa8pAApH3NnMC9TX9', function=ChoiceDeltaToolCallFunction(arguments='', name='read'), type='function' ) ] )

Here’s my implementation with the new Python SDK (handling the legacy function calls really should be separate logic, but…):

from collections import defaultdict tool_calls = [ ] index = 0 start = True for chunk in response: delta = chunk.choices[ 0 ].delta if not delta: break if not delta.function_call and not delta.tool_calls: if start: continue else: break start = False if delta.function_call: if index == len( tool_calls ): tool_calls.append( defaultdict( str ) ) if delta.function_call.name: tool_calls[ index ][ 'name' ] = delta.function_call.name if delta.function_call.arguments: tool_calls[ index ][ 'arguments' ] += ( delta.function_call.arguments ) elif delta.tool_calls: tool_call = delta.tool_calls[ 0 ] index = tool_call.index if index == len( tool_calls ): tool_calls.append( defaultdict( str ) ) if tool_call.id: tool_calls[ index ][ 'id' ] = tool_call.id if tool_call.function: if tool_call.function.name: tool_calls[ index ][ 'name' ] = tool_call.function.name if tool_call.function.arguments: tool_calls[ index ][ 'arguments' ] += ( tool_call.function.arguments )

Hope this helps.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3